package top.psjj.news.web;

import com.google.gson.Gson;
import top.psjj.news.po.TLink;
import top.psjj.news.po.TNews;
import top.psjj.news.po.TType;
import top.psjj.news.service.LinkService;
import top.psjj.news.service.NewsService;
import top.psjj.news.service.TypeService;
import top.psjj.news.service.impl.LinkServiceImpl;
import top.psjj.news.service.impl.NewsServiceImpl;
import top.psjj.news.service.impl.TypeServiceImpl;
import top.psjj.news.utils.ServiceProxyFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther:小陈的Java
 */
@WebServlet("/index")
public class IndexAction extends BasicServlet{
    private TypeService typeService =(TypeService) ServiceProxyFactory.getService(TypeServiceImpl.class);
    private NewsService newsService =(NewsService) ServiceProxyFactory.getService(NewsServiceImpl.class);
    private LinkService linkService =(LinkService) ServiceProxyFactory.getService(LinkServiceImpl.class);
    private  String baseUrl = "/WEB-INF/jsp/front/";
    public void indexUI(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        req.getRequestDispatcher(baseUrl+"index.jsp").forward(req,resp);
    }
    public void typeList(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        resp.setContentType("application/json;charset=utf-8");
        List<TType> types = typeService.queryAll();
        Gson gson = new Gson();
        String result = gson.toJson(types);
        resp.getWriter().print(result);
    }
    public void imgList(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        resp.setContentType("application/json;charset=utf-8");
        List<TNews> list = newsService.getImgList();
        Gson gson = new Gson();
        String result = gson.toJson(list);
        resp.getWriter().print(result);
    }
    public void getHeader(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        resp.setContentType("application/json;charset=utf-8");
        TNews news = newsService.getHeader();
        Gson gson = new Gson();
        String result = gson.toJson(news);
        resp.getWriter().print(result);
    }
    public void updateNews(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        resp.setContentType("application/json;charset=utf-8");
        List<TNews> list = newsService.getUpdateNews();
        Gson gson = new Gson();
        String result = gson.toJson(list);
        resp.getWriter().print(result);
    }
    public void hotNews(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        //1.设置响应数据格式
        resp.setContentType("application/json;charset=utf-8");
        //2.获得轮播图数据
        List<TNews> list = newsService.getHotNews();
        //3.将数据转为json数据
        Gson gson = new Gson();
        String result = gson.toJson(list);
        //4.响应数据
        resp.getWriter().print(result);
    }
    //新闻列表
    public void newsList(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        //1.设置响应数据格式
        resp.setContentType("application/json;charset=utf-8");
        //2.获得轮播图数据
        List<TType> list = typeService.queryAllRelationNews();
        //3.将数据转为json数据
        Gson gson = new Gson();
        String result = gson.toJson(list);
        //4.响应数据
        resp.getWriter().print(result);
    }
    //链接列表
    public void linkList (HttpServletRequest req,HttpServletResponse resp) throws Exception{
        //1.设置响应数据格式
        resp.setContentType("application/json;charset=utf-8");
        //2.获得链接列表
        List<TLink> list = linkService.queryAll();

        //3.将数据转为json数据
        Gson gson = new Gson();
        String result = gson.toJson(list);
        //4.响应数据
        resp.getWriter().print(result);
    }
}
